<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>性格测试首页</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <style type="text/css">
        .container{
            width: 420px;
            height: 320px;
            min-height: 320px;
            max-height: 320px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            padding: 20px;
            z-index: 130;
            border-radius: 8px;
            background-color: #fff;
            box-shadow: 0 3px 18px rgba(100, 0, 0, .5);
            font-size: 16px;
        }
        .close{
            background-color: white;
            border: none;
            font-size: 18px;
            margin-left: 410px;
            margin-top: -10px;
        }

        .layui-input{
            border-radius: 5px;
            width: 300px;
            height: 40px;
            font-size: 15px;
        }
        .layui-form-item{
            margin-left: -20px;
        }
        #logoid{
            margin-top: -16px;
            padding-left:150px;
            padding-bottom: 15px;
        }
        .layui-btn{
            margin-left: -50px;
            border-radius: 5px;
            width: 350px;
            height: 40px;
            font-size: 15px;
        }
        .verity{
            width: 120px;
        }
        .font-set{
            font-size: 13px;
            text-decoration: none;
            margin-left: 120px;
        }
        a:hover{
            text-decoration: underline;
        }

    </style>
</head>
<body>
<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="container">
        <button class="close" title="关闭"></button>
        <div class="layui-form-mid layui-word-aux">
            <img id="logoid" src="/image/test.png" height="35">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name"   lay-verify="required|myName" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" lay-verify="required|phone|number"   placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="testFormFilter">开始测试</button>
            </div>
        </div>
    </div>
</form>
</body>
    <script>
        layui.use(['form','layer','jquery'], function(){
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.jquery;
            //各种基于事件的操作，下面会有进一步介绍
            form.verify({
                myName: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(!new RegExp("^[\u4E00-\u9FA5]{2,4}$").test(value)){
                        return '姓名只能是2到4位的汉字';
                    }

                },
            });


            form.on('submit(testFormFilter)', function(data){
                console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
                //提交表单数据
                $.ajax({
                    url:"/tester/insert",
                    type: "POST",
                    dataType:"JSON",
                    contentType:"application/json;charset=UTF-8",
                    data:JSON.stringify(data.field),
                    success:function (ret) {
                        if(ret.code==0){
                            //提交成功之后，怎么办?显示所有的测试题
                            window.location.href="/test/toTest?testerId="+ret.data.id;
                        }else
                        {
                            layer.msg(ret.msg,{icon: 5});
                        }

                    }
                })
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        });
    </script>
</html>
